package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.dto.UniversityStudentsStatisticsDto;
import xyz.zhouzhaodong.crm.dto.UniversityStudentsStatisticsDto;
import xyz.zhouzhaodong.crm.entity.UniversityStudentsStatistics;
import xyz.zhouzhaodong.crm.entity.UniversityStudentsStatistics;
import xyz.zhouzhaodong.crm.entity.UniversityStudentsStatistics;
import xyz.zhouzhaodong.crm.mapper.UniversityMapper;
import xyz.zhouzhaodong.crm.mapper.UniversityStudentsStatisticsMapper;
import xyz.zhouzhaodong.crm.mapper.UniversityStudentsStatisticsMapper;
import xyz.zhouzhaodong.crm.service.IUniversityStudentsStatisticsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 高校-在校学生统计 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-06-22
 */
@Service
public class UniversityStudentsStatisticsServiceImpl extends ServiceImpl<UniversityStudentsStatisticsMapper, UniversityStudentsStatistics> implements IUniversityStudentsStatisticsService {

    @Resource
    UniversityStudentsStatisticsMapper universityStudentsStatisticsMapper;

    @Resource
    UniversityMapper universityMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage addUniversityStudentsStatistics(UniversityStudentsStatisticsDto universityStudentsStatisticsDto) {
        try {
            UniversityStudentsStatistics universityStudentsStatistics = dtoChange(universityStudentsStatisticsDto);
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            universityStudentsStatistics.setId(id);
            universityStudentsStatistics.setCreateUser(universityStudentsStatisticsDto.getStaffId());
            universityStudentsStatistics.setCreateTime(new Date());
            universityStudentsStatisticsMapper.insert(universityStudentsStatistics);
            return RestMessage.success(id);
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage deleteUniversityStudentsStatistics(UniversityStudentsStatisticsDto universityStudentsStatisticsDto) {
        try {
            UniversityStudentsStatistics universityStudentsStatistics = new UniversityStudentsStatistics();
            universityStudentsStatistics.setId(universityStudentsStatisticsDto.getId());
            universityStudentsStatistics.setUpdateUser(universityStudentsStatisticsDto.getStaffId());
            universityStudentsStatistics.setUpdateTime(new Date());
            universityStudentsStatistics.setFlag(1);
            universityStudentsStatisticsMapper.updateById(universityStudentsStatistics);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage updateUniversityStudentsStatistics(UniversityStudentsStatisticsDto universityStudentsStatisticsDto) {
        try {
            UniversityStudentsStatistics universityStudentsStatistics = dtoChange(universityStudentsStatisticsDto);
            universityStudentsStatistics.setUpdateUser(universityStudentsStatisticsDto.getStaffId());
            universityStudentsStatistics.setUpdateTime(new Date());
            universityStudentsStatisticsMapper.updateById(universityStudentsStatistics);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    public RestMessage selectUniversityStudentsStatistics(UniversityStudentsStatisticsDto universityStudentsStatisticsDto) {
        Integer flag = universityStudentsStatisticsDto.getFlag() == null ? 0 : universityStudentsStatisticsDto.getFlag();
        if (universityStudentsStatisticsDto.getPageNo() != 0 && universityStudentsStatisticsDto.getPageSize() != 0){
            Page<UniversityStudentsStatisticsDto> page = new Page<>(universityStudentsStatisticsDto.getPageNo(), universityStudentsStatisticsDto.getPageSize());
            universityStudentsStatisticsDto.setFlag(flag);
            IPage<UniversityStudentsStatisticsDto> list = universityStudentsStatisticsMapper.selectListPage(page, universityStudentsStatisticsDto);
            for(UniversityStudentsStatisticsDto u : list.getRecords()){
                u.setUniversity(universityMapper.selectById(u.getUniversityId()));
            }
            return RestMessage.success(list);
        } else {
            QueryWrapper<UniversityStudentsStatistics> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("university_id", universityStudentsStatisticsDto.getUniversityId());
            if(universityStudentsStatisticsDto.getYear() != null && !"".equals(universityStudentsStatisticsDto.getYear())){
                queryWrapper.eq("year", universityStudentsStatisticsDto.getYear());
            }
            if (universityStudentsStatisticsDto.getStatus() != null && !"".equals(universityStudentsStatisticsDto.getStatus())) {
                queryWrapper.like("status", universityStudentsStatisticsDto.getStatus());
            }
            queryWrapper.eq("flag", flag);
            List<UniversityStudentsStatistics> list = universityStudentsStatisticsMapper.selectList(queryWrapper);
            return RestMessage.success(list);
        }
    }

    private UniversityStudentsStatistics dtoChange(UniversityStudentsStatisticsDto universityStudentsStatisticsDto) {
        UniversityStudentsStatistics universityStudentsStatistics = new UniversityStudentsStatistics();
        universityStudentsStatistics.setId(universityStudentsStatisticsDto.getId());
        universityStudentsStatistics.setUniversityId(universityStudentsStatisticsDto.getUniversityId());
        universityStudentsStatistics.setYear(universityStudentsStatisticsDto.getYear());
        universityStudentsStatistics.setNumber(universityStudentsStatisticsDto.getNumber());
        universityStudentsStatistics.setName(universityStudentsStatisticsDto.getName());
        universityStudentsStatistics.setCountry(universityStudentsStatisticsDto.getCountry());
        universityStudentsStatistics.setEducation(universityStudentsStatisticsDto.getEducation());
        universityStudentsStatistics.setSpecialty(universityStudentsStatisticsDto.getSpecialty());
        universityStudentsStatistics.setAdviser(universityStudentsStatisticsDto.getAdviser());
        universityStudentsStatistics.setStudyDuration(universityStudentsStatisticsDto.getStudyDuration());
        universityStudentsStatistics.setStatus(universityStudentsStatisticsDto.getStatus());
        universityStudentsStatistics.setRemark(universityStudentsStatisticsDto.getRemark());
        universityStudentsStatistics.setFlag(universityStudentsStatisticsDto.getFlag());
        return universityStudentsStatistics;
    }

}
